<?php
	if ( sizeof($_POST) > 0 )
	{
		$eeRO_data = array('userid' => $_POST['userid'], 'user_pass' => $_POST['user_pass'], 'user_pass_copy' => $_POST['user_pass_copy'], 'email' => $_POST['email'], 'email_copy' => $_POST['email_copy'], 'sex' => $_POST['sex']);
		$eeRO_error = array();
	
		if ( !preg_match('/^[0-9a-z_]{4,23}$/i', $eeRO_data['userid']) )
		{
			$eeRO_error['userid'] = true;
		}
		
		if ( !preg_match('/^[0-9a-z_]{4,32}$/i', $eeRO_data['user_pass']) )
		{
			$eeRO_error['user_pass'] = true;
		}
		
		if ( $eeRO_data['user_pass'] != $eeRO_data['user_pass_copy'] )
		{
			$eeRO_error['user_pass_copy'] = true;
		}
		
		if ( !filter_var($eeRO_data['email'], FILTER_VALIDATE_EMAIL) )
		{
			$eeRO_error['email'] = true;
		}
		
		if ( isset($eeRO_data['email']{39}) )
		{
			$eeRO_error['email_len'] = true;
		}
		
		if ( $eeRO_data['email'] != $eeRO_data['email_copy'] )
		{
			$eeRO_error['email_copy'] = true;
		}
		
		if ( $eeRO_data['sex'] != 'M' && $eeRO_data['sex'] != 'F' )
		{
			$eeRO_error['sex'] = true;
		}
	
		if ( sizeof($eeRO_error) > 0 )
		{
			$eeRO_view->assign('error', $eeRO_error);
			$eeRO_view->assign('module', array('title' => 'Регистрация', 'template' => 'account_register.html')); 
		}
		else
		{
			$eeRO_base_ragnarok->query('SELECT `account_id` FROM `login` WHERE `userid`=:userid OR `email`=:email', array(':userid' => $eeRO_data['userid'], ':email' => $eeRO_data['email']));
			if ( $eeRO_base_ragnarok->num() > 0 )
			{
				$eeRO_view->assign('error', array('userid_exists' => true));
				$eeRO_view->assign('module', array('title' => 'Регистрация', 'template' => 'account_register.html')); 
			}
			else
			{
				$eeRO_data['password'] = ( $eeRO_config->options['md5'] ) ? md5($eeRO_data['user_pass']) : $eeRO_data['user_pass'];
				$eeRO_base_ragnarok->query('INSERT INTO `login` (`userid`, `user_pass`, `email`, `sex`, `level`) VALUES (:userid, :user_pass, :email, :sex, :level)', array(':userid' => $eeRO_data['userid'], ':user_pass' => $eeRO_data['password'], ':email' => $eeRO_data['email'], ':sex' => $eeRO_data['sex'], ':level' => 0));
				$eeRO_view->assign('module', array('title' => 'Регистрация', 'template' => 'account_register_yes.html')); 
			}
		}
	}
	else
	{
		$eeRO_view->assign('module', array('title' => 'Регистрация', 'template' => 'account_register.html')); 
	}